package com.his.server.account;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.his.server.consts.ServerConsts;
import com.his.server.dao.iface.UserInfoDAO;
import com.his.server.exception.DAOException;
import com.his.server.exception.DBException;
import com.his.server.jdbc.DBConnectionPool;
import com.his.server.logicdata.UserInfoLogicData;
import com.his.server.utils.dao.DAOFactory;
import com.his.server.utils.log.LogUtilFactory;
import com.his.server.utils.log.LogUtils;
import com.his.server.utils.main.BaseContext;

import com.opensymphony.xwork2.ActionSupport;

public class UserLoginAction extends ActionSupport {
	
    private static final LogUtils log = LogUtilFactory.getLog(UserLoginAction.class);
	
	@Override
    public String execute() throws Exception {
    	
	    if (log.isDebugEnabled()) {
	        log.writeDebug("START execute() {");
	    }
	    
	    UserInfoLogicData userInfoLogicData = 
	    		(UserInfoLogicData) BaseContext.getAtribute(ServerConsts.SK_USERINFOLOGICDATA);
	    DBConnectionPool dbConnectionPool = new DBConnectionPool();
	    Connection Connection = null;
	    PreparedStatement psmt = null;
	    ResultSet rs = null;
	    UserInfoDAO userInfoDAO = null;
	    Class userInfoDAO_C = null;
	    String name =  ServerConsts.EMPTY;
	    String password =  ServerConsts.EMPTY;
	    int usercount = 0;
	    
		if (userInfoLogicData != null) {
			name = userInfoLogicData.getName();
			password = userInfoLogicData.getPassword();
		}
		
		try {
			Connection = dbConnectionPool.getConnction();
			userInfoDAO_C = (Class)DAOFactory.getDao(UserInfoDAO.class);
			userInfoDAO = (UserInfoDAO) userInfoDAO_C.newInstance();
			//userInfoDAO = (UserInfoDAO)DAOFactory.getDao(UserInfoDAO.class);
			userInfoDAO.setUserName(name);
			userInfoDAO.setPassword(password);
			userInfoDAO.doSqlExecute(Connection);
			usercount = userInfoDAO.getUserCount();
		} catch (DBException dbex) {
			
		    if (log.isDebugEnabled()) {
		        log.writeDebug("END execute() }");
		    }
			
			dbex.printStackTrace();
			
		} catch (DAOException daoex) {
			
		    if (log.isDebugEnabled()) {
		        log.writeDebug("END execute() }");
		    }
		    
			daoex.printStackTrace();
		}finally {
			
			if (userInfoDAO != null) {
				try {
					userInfoDAO.releasePstmt();
				} catch (DBException dbex) {
					
				    if (log.isDebugEnabled()) {
				        log.writeDebug("END execute() }");
				    }
				    
					dbex.printStackTrace();
				}
			}

			if (dbConnectionPool != null) {
				try {
					dbConnectionPool.releaseConnction(Connection);
				} catch (DBException dbex) {
					
				    if (log.isDebugEnabled()) {
				        log.writeDebug("END execute() }");
				    }
				    
					dbex.printStackTrace();
				}
			}						
		}
		
	    if (log.isDebugEnabled()) {
	        log.writeDebug("END execute() }");
	    }
	    
		if (usercount == 1){
			
			// login success
			userInfoLogicData.setLoginflag(ServerConsts.LOGINFLAG_0);
			BaseContext.setAtribute(ServerConsts.SK_USERINFOLOGICDATA, userInfoLogicData);
			return ServerConsts.LOGIN_SUCCESS;
			
		} else {
			
			// login failure
			userInfoLogicData.setLoginflag(ServerConsts.LOGINFLAG_1);
			BaseContext.setAtribute(ServerConsts.SK_USERINFOLOGICDATA, userInfoLogicData);
			return ServerConsts.LOGIN_FAILURE;
		}      
    }
}